
Introduce: In razor you have to replace HTML markup code. As like the opening & ending tags in HTML, replace by “@” symbol. So therefor, razor decrease and simplify your coding and it is easy to learn.
Step : 1
- Open Visual studio
- Click on “File”>New>Project>Visual c#>Web> then provide your project name> then select “ok”> then click “MVC”>ok.
- Create new controller name “RazorView”
- Add view name “RazorSyntax”
Step : 2
Example 1 : Single statement
@{
var message = "TestError";
}
<h4>@message</h4>
Example 2 : Multiple statement
@{
int num1 = 20;
int num2 = 30;
int sum = num1 + num2;
}
<h4>Result is :@sum</h4>
Example 3 : Conditional statement
@{
int num3 = 40;
int num4 = 50;
if (num3 > num4)
{
<p>greater Number is: @num3</p>
}
else
{
<p>greater number is: @num4</p>
}
}
Example 4 : Looping constructs “for” loop.
@{
for (int i = 0; i <= 10; i++)
{
<p>@i</p>
}
}
Example 5 : Looping constructs “foreach” loop and use of array.
@{
int[] Array = { 10, 20, 30, 40 };
foreach (var PrintArray in Array)
{
<p>@PrintArray</p>
}
}
Example 6 : Use of object
Date:@System.DateTime.Now.Date.ToString("D")<br />
Time:@System.DateTime.Now.Date.ToString("T")<br />
Hour:@System.DateTime.Now.Hour<br />
Min:@System.DateTime.Now.Minute<br />
Sce:@System.DateTime.Now.Second<br />